import { PaginationParam, PaginationResult } from "../../models";
import { AttrViewReq, EquipmentAttrItem, ParEquipmentAttrItem, ParEquipmentTypeGroup } from "./models";
import req, { RequsetOptons } from "/@/api/req";
// 企业
export enum Api {
  addEquipmentParamGroups = "api/aiot/equipmentParamGroups/addEquipmentParamGroups",
  deleteByIds = "api/aiot/equipmentParamGroups/deleteByIds",
  equipmentGroupPage = "api/aiot/equipmentParamGroups/page",
  updateEquipmentParamGroups = "api/aiot/equipmentParamGroups/updateEquipmentParamGroups",
  // 属性接口
  attrAdd = "api/aiot/equipmentTypeAttributes/addEquipmentTypeAttributes",
  attrDel = "api/aiot/equipmentTypeAttributes/deleteByIds",
  attrGet = "api/aiot/equipmentTypeAttributes/getById",
  attrPage = "api/aiot/equipmentTypeAttributes/page",
  attrUpdate = "api/aiot/equipmentTypeAttributes/updateEquipmentTypeAttributes",
  updateDisplayOrder = "api/aiot/equipmentTypeAttributes/updateDisplayOrder",
  // 分组接口
  funcGrpGet = "api/aiot/equipmentParamGroups/getById",
  funcGrpPage = "api/aiot/equipmentParamGroups/page",
  funcGrpUpdate = "api/aiot/equipmentParamGroups/updateEquipmentParamGroups",
  funcGrpDel = "api/aiot/equipmentParamGroups/deleteByIds",
  funcGrpAdd = "api/aiot/equipmentParamGroups/addEquipmentParamGroups",
  // 设备属性
  equipmentAttrPage = "api/aiot/equipmentAttributes/page",
  bandEquipmentAttributes = "api/aiot/equipmentAttributes/bandEquipmentAttributes",
  unbandEquipmentAttributes = "api/aiot/equipmentAttributes/unbandEquipmentAttributes",
  changeEquipmentTypeAttributesStatus = "api/aiot/equipmentTypeAttributes/changeEquipmentTypeAttributesStatus",
  // 批量设备类型属性视图
  changeEquipmentTypeAttributesViewBatch = "api/aiot/equipmentTypeAttributes/changeEquipmentTypeAttributesViewBatch",
  importAttr = "api/aiot/equipmentType/import",
  exportAttr = "api/aiot/equipmentType/export",
  // api/aiot/equipmentType/import
}
// 平台
export enum Api_system {
  attrAdd = "api/aiot/defaultEquipmentTypeAttributes/addEquipmentTypeAttributes",
  attrDel = "api/aiot/defaultEquipmentTypeAttributes/deleteByIds",
  attrGet = "api/aiot/defaultEquipmentTypeAttributes/getById",
  attrPage = "api/aiot/defaultEquipmentTypeAttributes/page",
  attrUpdate = "api/aiot/defaultEquipmentTypeAttributes/updateEquipmentTypeAttributes",
  updateDisplayOrder = "api/aiot/defaultEquipmentTypeAttributes/updateDisplayOrder",
  // 设备分组
  addEquipmentParamGroups = "api/aiot/equipmentParamGroups/addEquipmentParamGroups",
  deleteByIds = "api/aiot/equipmentParamGroups/deleteByIds",
  equipmentGroupPage = "api/aiot/equipmentParamGroups/page",
  updateEquipmentParamGroups = "api/aiot/equipmentParamGroups/updateEquipmentParamGroups",
  changeEquipmentTypeAttributesStatus = "api/aiot/defaultEquipmentTypeAttributes/changeEquipmentTypeAttributesStatus",
}

export function equipmentGroupPage(equipmentTypeId: string) {
  return req.commonPost<PaginationResult<ParEquipmentTypeGroup>>(Api.equipmentGroupPage, {
    size: 999,
    current: 1,
    model: { equipmentTypeId },
  });
}
export function equipmentGroupPageSys(equipmentTypeId: string) {
  return req.commonPost<PaginationResult<ParEquipmentTypeGroup>>(Api_system.equipmentGroupPage, {
    size: 999,
    current: 1,
    model: { equipmentTypeId },
  });
}
export function addEquipmentParamGroups(data: ParEquipmentTypeGroup) {
  return req.commonPost<boolean>(Api.addEquipmentParamGroups, data);
}
export function updateEquipmentParamGroups(data: ParEquipmentTypeGroup) {
  return req.commonPost<boolean>(Api.updateEquipmentParamGroups, data);
}
export function attrAdd(data: ParEquipmentAttrItem) {
  return req.commonPost<boolean>(Api.attrAdd, data);
}
export function attrAddSys(data: ParEquipmentAttrItem) {
  return req.commonPost<boolean>(Api_system.attrAdd, data);
}
// 企业修改设备属性
export function attrUpdate(data: ParEquipmentAttrItem) {
  return req.commonPost<boolean>(Api.attrUpdate, data);
}
// 平台修改设备属性
export function attrUpdateSys(data: ParEquipmentAttrItem) {
  return req.commonPost<boolean>(Api_system.attrUpdate, data);
}
export function bandEquipmentAttributes(data: { equipmentAttributeId: string; gatewayDatapointId: string }) {
  return req.commonPost<boolean>(Api.bandEquipmentAttributes, data);
}

export function attrGetSys(id: string) {
  return req.post<EquipmentAttrItem>({
    data: {},
    url: Api_system.attrGet,
    params: { id },
  });
}
export function attrGet(id: string) {
  return req.post<EquipmentAttrItem>({
    data: {},
    url: Api.attrGet,
    params: { id },
  });
}
// export function unbandEquipmentAttributes(equipmentAttributeId: string) {
//   return req.commonPost<boolean>(Api.unbandEquipmentAttributes, { equipmentAttributeId });
// }
export function unbandEquipmentAttributes(equipmentAttributeId: string) {
  return req.post<boolean>({
    url: Api.unbandEquipmentAttributes,
    params: { equipmentAttributeId },
  });
}
export function changeEquipmentTypeAttributesStatus(data: ParEquipmentAttrItem) {
  return req.post<boolean>({
    url: Api.changeEquipmentTypeAttributesStatus,
    data,
  });
}
// 企业获取属性分页接口
export function attrPage(data: PaginationParam<ParEquipmentAttrItem>) {
  return req.post<PaginationResult<ParEquipmentAttrItem>>({
    url: Api.attrPage,
    data,
  });
}
// 批量修改设备类型属性视图
export function changeEquipmentTypeAttributesViewBatch(data: AttrViewReq) {
  return req.post<boolean>({
    url: Api.changeEquipmentTypeAttributesViewBatch,
    data,
  });
}
// 导入
export function importAttr(data: FormData, equipmentTypeId: string) {
  return req.post<boolean>(
    {
      url: Api.importAttr,
      data,
      params: { equipmentTypeId },
    },
    {
      useGlobalLoading: false,
    }
  );
}
// // 导入
// export function exportAttr(equipmentTypeId: string) {
//   return req.post<Blob>(
//     {
//       url: Api.exportAttr,
//       data: { equipmentTypeId },
//       responseType: "blob",
//     },
//     {
//       useGlobalLoading: false,
//     }
//   );
// }

export function exportAttr(equipmentTypeId: string, options: RequsetOptons) {
  return req.post<Blob>(
    {
      url: Api.exportAttr,
      responseType: "blob",
      data: { equipmentTypeId },
    },
    options
  );
}

export function updateDisplayOrder(
  data: {
    displayOrder: number;
    dragPosition: "UP" | "DOWN";
    equipmentTypeAttributeId?: string;
    defaultEquipmentTypeAttributeId?: string;
  },
  isDefault = false
) {
  return req.commonPost<boolean>(isDefault ? Api_system.updateDisplayOrder : Api.updateDisplayOrder, data, {
    useGlobalLoading: true,
  });
}

export function exportTemp(options: RequsetOptons) {
  // http://192.1.5.251:9000/sl-aiot//equipmentType/2023/06/02/b69d2b37e0574d2ebdd442a77e997744.xlsx

  // http://aiot.shenling.com/sl-aiot/equipmentType/2023/06/02/b69d2b37e0574d2ebdd442a77e997744.xlsx
  return req.get<Blob>(
    {
      url:
        import.meta.env.MODE === "development"
          ? "https://192.1.5.251:9000/sl-aiot//equipmentType/2023/06/02/b69d2b37e0574d2ebdd442a77e997744.xlsx"
          : "https://aiot.shenling.com/sl-aiot/equipmentType/2023/06/02/b69d2b37e0574d2ebdd442a77e997744.xlsx",

      responseType: "blob",
    },
    options
  );
}
